package ua.at.tsvetkov.webservices;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.message.BasicNameValuePair;

import ua.at.tsvetkov.net.NetCheckerHelper;
import ua.at.tsvetkov.util.Log;
import android.net.http.AndroidHttpClient;

/**
 * Abstract class for easy create http requests to server
 * 
 * @author Alexandr Tsvetkov 18.09.2012
 */
public abstract class HttpClientRequests {

	public static final int				DEFAULT_TIMEOUT	= 3000;
	public static final String			HTTP_USER_AGENT	= "Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; Nexus One Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";

	private static int					timeout				= DEFAULT_TIMEOUT;
//	private static DefaultHttpClient	httpClient;

	/**
	 * Get current timeout
	 * 
	 * @return
	 */
	public static int getTimeout() {
		return timeout;
	}

	/**
	 * Set current timeout
	 * 
	 * @param time
	 */
	public static void setTimeout(int time) {
		timeout = time;
	}

	/**
	 * Set default timeout
	 * 
	 * @param time
	 */
	public static void setDefaultTimeout() {
		timeout = DEFAULT_TIMEOUT;
	}

	/**
	 * Check connection with server
	 * 
	 * @return
	 */
	public static boolean isHaveConnection() {
		return NetCheckerHelper.isHaveConnection(UrlsConstructor.getServerRoot());
	}

	/**
	 * Check connection with server
	 * 
	 * @return
	 */
	public static boolean isHaveConnection(String url) {
		return NetCheckerHelper.isHaveConnection(url);
	}

	/**
	 * Send GET request.
	 * 
	 * @param url
	 * @return InputStream for processing.
	 * @throws IOException
	 */
	protected static InputStream sendGetRequest(String url) throws IOException {
		URL myurl = new URL(url);
		HttpURLConnection connection = (HttpURLConnection) myurl.openConnection();
		connection.setReadTimeout(timeout);
		connection.setConnectTimeout(timeout);
		connection.setRequestMethod("GET");
		connection.connect();
		return connection.getInputStream();
	}

	/**
	 * Send POST request without waiting a reply in separate thread.
	 * 
	 * @param request
	 */
	public static void sendPostRequestInThread(final String request, final List<BasicNameValuePair> nameValuePairs) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				sendPostRequest(request, nameValuePairs);
			}

		}).start();
	}

	/**
	 * Send POST request.
	 * 
	 * @param request
	 * @param nameValuePairs
	 * @return
	 */
	public static String sendPostRequest(String request, List<BasicNameValuePair> nameValuePairs) {
		AndroidHttpClient httpClient = AndroidHttpClient.newInstance(HTTP_USER_AGENT);
		try {
			HttpPost httpPost = new HttpPost(request);

			if (nameValuePairs != null) {
				httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
				for (BasicNameValuePair basicNameValuePair : nameValuePairs) {
					Log.v(basicNameValuePair.getName() + "=" + basicNameValuePair.getValue());
				}
			}
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			return httpClient.execute(httpPost, responseHandler);
		} catch (IOException e) {
			Log.e(e);
		} finally {
			httpClient.close();
		}
		return null;
	}

//	/**
//	 * Return default http client
//	 * 
//	 * @return DefaultHttpClient
//	 */
//	protected static DefaultHttpClient getHttpClient() {
//		if (httpClient == null) {
//			createDefaultHttpClient();
//		}
//		return httpClient;
//	}
//
//	protected static void createDefaultHttpClient() {
//		HttpParams httpParameters = new BasicHttpParams();
//		HttpConnectionParams.setConnectionTimeout(httpParameters, timeout);
//		HttpConnectionParams.setSoTimeout(httpParameters, timeout);
//		httpClient = new DefaultHttpClient(httpParameters);
//	}

}
